package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.List;

import com.pesking.core.exception.SystemException;
import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.util.StringUtils;
import com.pesking.unified.auth.UserManage;

public class UserNameSqlParameterHandler extends SqlParameterProcessor {

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"USER_NAME"});
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String name = sqlParameter.getName();
		if (StringUtils.isBlank(name)){
			throw new SystemException("[USER_NAME:user_id_field],user_id_field指参数名");
		}
		String userId = parameter.getParameter(name);
		if (StringUtils.isBlank(userId)){
			sbSQL.append("''");
		}
		else{
			String []userIds = new String[] {userId};
			List<String> userNames = UserManage.getUserService().getUserNameByIds(userIds);
			if (userNames != null && !userNames.isEmpty()) {
				sbSQL.append("'").append(userNames.get(0)).append("'");
			}
			else {
				sbSQL.append("''");
			}
		}
	}

}
